package ytl;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Finance {

    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    JScrollPane scrollpane = new JScrollPane();
    String Button_Command = "Sales Report";
    JTextArea text = new JTextArea();
    JFrame Financial_Frame = new JFrame("Financial Report");
    com.toedter.calendar.JDateChooser Choose_Start_Date = new com.toedter.calendar.JDateChooser();
    com.toedter.calendar.JDateChooser Choose_End_Date = new com.toedter.calendar.JDateChooser();

    JLabel Error_finance = new JLabel("");
    
    ImageIcon general_finance_button_img = new ImageIcon("pictures/finance/general_finance_button.png");
    ImageIcon general_finance_buttonr_img = new ImageIcon("pictures/finance/general_finance_button_r.png");
    ImageIcon general_finance_buttonp_img = new ImageIcon("pictures/finance/general_finance_button_p.png");
        
    ImageIcon completed_transactions_button_img = new ImageIcon("pictures/finance/completed_transactions_button.png");
    ImageIcon completed_transactions_buttonr_img = new ImageIcon("pictures/finance/completed_transactions_button_r.png");
    ImageIcon completed_transactions_buttonp_img = new ImageIcon("pictures/finance/completed_transactions_button_p.png");

    ImageIcon product_statistics_button_img = new ImageIcon("pictures/finance/product_statistics_button.png");
    ImageIcon product_statistics_buttonr_img = new ImageIcon("pictures/finance/product_statistics_button_r.png");
    ImageIcon product_statistics_buttonp_img = new ImageIcon("pictures/finance/product_statistics_button_p.png");

    ImageIcon calculated_financials_button_img = new ImageIcon("pictures/finance/calculated_financials_button.png");
    ImageIcon calculated_financials_buttonr_img = new ImageIcon("pictures/finance/calculated_financials_button_r.png");
    ImageIcon calculated_financials_buttonp_img = new ImageIcon("pictures/finance/calculated_financials_button_p.png");

    JButton Report_button = new JButton(general_finance_button_img);
    JButton Financials_button = new JButton(calculated_financials_button_img);
    JButton Statistics_button = new JButton(product_statistics_button_img);
    JButton Transactions_button = new JButton(completed_transactions_button_img);
    JLabel Start_Date = new JLabel("                              Start: ");
    JLabel End_Date = new JLabel("                              End:   ");
    
    Box finance_data_box = Box.createVerticalBox();
    Box finance_data_box2 = Box.createHorizontalBox();
    Box finance_data_box3 = Box.createHorizontalBox();
    Box finance_text_box = Box.createVerticalBox();
    
    JLabel logo = new JLabel();
    String str = new String();
    JLabel Error_logo = new JLabel();
    JFrame Error_Frame = new JFrame("Error !!");
    
    Container Error_contentPane = Error_Frame.getContentPane();
    Point pt = new Point();
    
    FinanceController financeController = new FinanceController();
    // </editor-fold>
    
    public void Init_Finance(){
        
        scrollpane.setViewportView(text);
        Error_Frame.setSize(new Dimension(280, 130));
        Error_Frame.setResizable(false);
        Error_logo.setIcon(new javax.swing.ImageIcon("pictures/error/error_general.png"));
        Error_contentPane.setBackground(Color.white);
        Error_contentPane.add(Error_logo,BorderLayout.BEFORE_LINE_BEGINS);
        Error_finance.setForeground(Color.red);
        
        logo.setIcon(new javax.swing.ImageIcon("pictures/finance/YTL Logo (finance250).png"));
        Start_Date.setFont(new java.awt.Font("Verdana", 0, 12));
        End_Date.setFont(new java.awt.Font("Verdana", 0, 12));
        
        
        Report_button.setRolloverIcon(general_finance_buttonr_img);
        Report_button.setPressedIcon(general_finance_buttonp_img);
        Report_button.setActionCommand("Generate Sales Report");
        Report_button.addActionListener(new Action());
        Main_Menu.Button_Options(Report_button);
        
        Transactions_button.setRolloverIcon(completed_transactions_buttonr_img);
        Transactions_button.setPressedIcon(completed_transactions_buttonp_img);
        Transactions_button.setActionCommand("View Completed Transactions");
        Transactions_button.addActionListener(new Action());
        Main_Menu.Button_Options(Transactions_button);
        
        Statistics_button.setRolloverIcon(product_statistics_buttonr_img);
        Statistics_button.setPressedIcon(product_statistics_buttonp_img);
        Statistics_button.setActionCommand("View Product Statistics");
        Statistics_button.addActionListener(new Action());
        Main_Menu.Button_Options(Statistics_button);
        
        Financials_button.setRolloverIcon(calculated_financials_buttonr_img);
        Financials_button.setPressedIcon(calculated_financials_buttonp_img);
        Financials_button.setActionCommand("View Calculated Financials");
        Financials_button.addActionListener(new Action());
        Main_Menu.Button_Options(Financials_button);
        
        finance_data_box.setBorder(BorderFactory.createEmptyBorder(0,-109,0,0));
        finance_data_box2.setBorder(BorderFactory.createEmptyBorder(5,0,5,20));
        finance_data_box3.setBorder(BorderFactory.createEmptyBorder(5,0,5,20));
        
        finance_text_box.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
        text.setFont(new java.awt.Font("Verdana", 0, 15));
        
    }
    
    protected JComponent Finance_Data() {
        finance_data_box.removeAll();
        finance_data_box2.removeAll();
        finance_data_box3.removeAll();
        finance_data_box.add(logo);
        finance_data_box.add(Box.createVerticalStrut(10));
        finance_data_box.add(Report_button);
        finance_data_box.add(Box.createVerticalStrut(5));
        finance_data_box.add(Transactions_button);
        finance_data_box.add(Box.createVerticalStrut(5));
        finance_data_box.add(Statistics_button);
        finance_data_box.add(Box.createVerticalStrut(5));
        finance_data_box.add(Financials_button);
        finance_data_box2.add(Start_Date);
        finance_data_box2.add(Choose_Start_Date);
        finance_data_box3.add(End_Date);
        finance_data_box3.add(Choose_End_Date);
        finance_data_box.add(finance_data_box2);
        finance_data_box.add(finance_data_box3);
        return finance_data_box;
    }

    protected JComponent Finance_Text() {
        finance_text_box.removeAll();
        finance_text_box.add(Box.createVerticalStrut(5));
        finance_text_box.add(scrollpane);
        return finance_text_box;
    }

    public class Action implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent ae){
            str = ae.getActionCommand();
            try{
                if(Choose_Start_Date.getDate().after(Choose_End_Date.getDate()))
                {
                    Error();
                }
                else{
                    if(str.equals("Generate Sales Report")){
                    text.setText("");
                    Button_Command = "Sales Report";
                    text.append(financeController.GeneralFinancialReport(Main_Menu.DBConnection, Choose_Start_Date.getCalendar(), Choose_End_Date.getCalendar()));
                }
                else if(str.equals("View Completed Transactions")){
                    text.setText("");
                    Button_Command = "Transactions";
                    text.append(financeController.CompletedTransactionsReport(Main_Menu.DBConnection, Choose_Start_Date.getCalendar()));
                }
                else if(str.equals("View Product Statistics")){
                    text.setText("");
                    Button_Command = "Statistics";
                    text.append(financeController.ProductStatisticsReport(Main_Menu.DBConnection, Choose_Start_Date.getCalendar(), Choose_End_Date.getCalendar()));
                }
                else if(str.equals("View Calculated Financials")){
                    text.setText("");
                    Button_Command = "Financials";
                    text.append(financeController.CalculatedFinancialsReport(Main_Menu.DBConnection, Choose_Start_Date.getCalendar(), Choose_End_Date.getCalendar()));
                }
                }
            }catch (NullPointerException e){
                Error();
            }
            
        }
    }
    
    public static void Finance() {
           
    }
    
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Finance();
            }
        });
    }
    
    public void Error(){
        pt = Main_Menu.frame.getLocation();
        Error_Frame.setLocation(pt.x + 280,pt.y + 140);
        try{
            Error_thread threads = new Error_thread();
            threads.start();
        }catch(IllegalThreadStateException e){
        }
    }
    
    class Error_thread extends Thread {
        
        @Override
        public void run() {
            try {
                Error_Frame.setVisible(true);
                if(Main_Menu.Systems.sound==true)
                try {
                AudioClip no = Applet.newAudioClip(new URL("file:pictures/no.wav"));
                no.play();
                } catch (MalformedURLException ex) {
                }
                Thread.sleep(1000);
                Error_Frame.setVisible(false);
            } catch (InterruptedException e) {
    }}}
}
